var logger = global.logger;


module.exports = function(account, hbsocket){
    return new User(account, hbsocket);
}

var User = function(account, hbsocket){
    this.account = account;
    this.roomId = '';
    this.state = this.UserState.InHall;
    this.hbsocket = hbsocket;
    this.allcards = [];
    this.cardgroups = [];
    this.name = '';
    this.setongroup = 0;
    this.gamecards = [];
}

User.prototype.UserState = {
    InHall: 0,
    Prepate: 1,
    Game: 2,
}

User.prototype.push = function(route, msg){
    if(this.hbsocket){
        this.hbsocket.push(route, msg);
    }
    else{
        logger.warn('user have not bind socket: ' + this.account);
    }
}

User.prototype.kickOff = function(info){
    if(this.hbsocket){
        this.hbsocket.kickOff(info);
    }
    else{
        logger.warn('user have not bind socket: ' + this.account);
    }
}

User.prototype.getCardById = function(id){
    for(var i = 0; i < this.allcards.length; i++){
        if(this.allcards[i].id == id){
            return this.allcards[i];
        }
    }

    logger.warn('自己的卡组里没有这个id的卡片： ' + id);
}

User.prototype.initGameCards = function(){
    this.gamecards = []
    var setongroup = this.cardgroups[this.setongroup];
    console.log(this.cardgroups)
    for(var i = 0; i < setongroup.cards.length; i++){
        var find = this.getCardById(setongroup.cards[i]);
        if(find){
            this.gamecards.push(find);
        }
    }
}

User.prototype.toData = function(){
    return JSON.stringify({
        allcards: this.allcards,
        cardgroups: this.cardgroups,
        name: this.name,
        account: this.account,
        setongroup: this.setongroup,
    });
}